home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 14.7 KB | 412 lines | [TEXT/ttxt] |
- in module DigitalClockModule
-
- --*******************************************************************************
- --* Class name: DigitalClock
- --*
- --* Inherits from: TextPresenter
- --* Class type: Concrete
- --* Component: User Interface
- --*
- --* Description: This is a subclass of TextPresenter whose target string has
- --* the format "hour:minute:second AM/PM". It can display either
- --* standard ("01:49:22 PM") or military time ("13:49:22:54").
- --* The setTime method allows you to toggle between the two
- --* by specifying the format. To start the clock, call the
- --* startClock method. It sets the time then starts callbacks
- --* which updates the clock. The tick substring of the target
- --* string gets updated every 60th of a second. The second,
- --* minute and hour substrings get updated every second, on the
- --* minute and on the hour respectively. The mode IV
- --* allows you to toggle between the clock and stopwatch modes.
- --* To start, stop and reset the stopwatch, call the start, stop
- --* and reset methods respectively.
- --*
- --* Usage: dc := new DigitalClock format:@12hour
- --*
- --* IVs: clock
- --* format -- format of the time display (@12hour, @24hour)
- --* _mode -- mode of the clock (@clock, @stopwatch)
- --*
- --* Methods: setHours
- --* setMinutes
- --* setSeconds
- --* setTicks
- --* setTime
- --* reset
- --* start
- --* stop
- --* modeGetter
- --* modeSetter
- --* updateClock
- --* startClock
- --* init
- --*
- --* Required files: none
- --*
- --* Notes: If using text attributes other than the default, you
- --* must explicitly set the text presenters boundary and
- --* leading (if using a different font size).
- --*
- --* Author: Su Quek - Kaleida Labs, Inc.
- --*******************************************************************************
- class DigitalClock (TextPresenter)
- inst vars
- clock : (new Clock scale:60)
- format
- _mode
- end
-
- --*=============================================================================*
- --* Method name: setHours
- --* Class: DigitalClock
- --* Usage: setHours self
- --*-----------------------------------------------------------------------------*
- --* Description: Determines the appropriate AM/PM and hour based on the time
- --* and format and sets their respective substrings within the
- --* target string.
- --*=============================================================================*
- method setHours self {class DigitalClock} ->
- (
- local theString := self.target
- local theHour := self.clock.time.hours
- local hourString
- local amPMString
-
- --*=========================================================================*
- --* Determine the hour and AM/PM based on the time and format.
- --*=========================================================================*
- if (self.format = @12hour) then
- (
- if (theHour < 12) then
- amPMString := " AM"
- else
- amPMString := " PM"
-
- theHour := mod theHour 12
- if (theHour = 0) do
- theHour := 12
- )
- else
- amPMString := " "
-
- --*=========================================================================*
- --* Update the AM/PM substring by replacing its value within the target
- --* string.
- --*=========================================================================*
- deleteFromTo theString 8 11
- insertAt theString amPMString 8
-
- --*=========================================================================*
- --* Add a zero to the hour string if time is before 10:00 AM (eg 09:xx:xx AM)
- --*=========================================================================*
- if (theHour < 10) then
- hourString := "0" + (theHour as string)
- else
- hourString := theHour as string
-
- --*=========================================================================*
- --* Update the hour substring by replacing its value within the target
- --* string.
- --*=========================================================================*
- deleteFromTo theString 0 2
- insertAt theString hourString 0
-
- return theHour
- )
-
- --*=============================================================================*
- --* Method name: setMinutes
- --* Class: DigitalClock
- --* Usage: setMinutes self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the minute substring within the target string.
- --*=============================================================================*
- method setMinutes self {class DigitalClock} ->
- (
- local theString := self.target
- local theMin := self.clock.time.minutes
- local minString
-
- --*=========================================================================*
- --* Add a zero to the minute string if minute < 10 (eg xx:09:xx:xx)
- --*=========================================================================*
- if (theMin < 10) then
- minString := "0" + (theMin as string)
- else
- minString := theMin as string
-
- --*=========================================================================*
- --* Update the minute substring by replacing its value within the target
- --* string.
- --*=========================================================================*
- deleteFromTo theString 3 5
- insertAt theString minString 3
-
- return theMin
- )
-
- --*=============================================================================*
- --* Method name: setSeconds
- --* Class: DigitalClock
- --* Usage: setSeconds self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the second substring within the target string.
- --*=============================================================================*
- method setSeconds self {class DigitalClock} ->
- (
- local theString := self.target
- local theSec := self.clock.time.seconds
- local secString
-
- --*=========================================================================*
- --* Add a zero to the second string if second < 10 (eg xx:xx:09:xx)
- --*=========================================================================*
- if (theSec < 10) then
- secString := "0" + (theSec as string)
- else
- secString := theSec as string
-
- --*=========================================================================*
- --* Update the second substring by replacing its value within the target
- --* string.
- --*=========================================================================*
- deleteFromTo theString 6 8
- insertAt theString secString 6
-
- return theSec
- )
-
- --*=============================================================================*
- --* Method name: setTicks
- --* Class: DigitalClock
- --* Usage: setTicks self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the tick substring within the target string.
- --*=============================================================================*
- method setTicks self {class DigitalClock} ->
- (
- if (self.mode = @clock) do
- return
-
- local theString := self.target
- local theTick := self.clock.time.ticks
- local tickString
-
- --*=========================================================================*
- --* Add a zero to the tick string if tick < 10 (eg xx:xx:xx:09)
- --*=========================================================================*
- if (theTick < 10) then
- tickString := ":0" + (theTick as string)
- else
- tickString := ":" + (theTick as string)
-
- --*=========================================================================*
- --* Update the tick substring by replacing its value within the target
- --* string.
- --*=========================================================================*
- deleteFromTo theString 8 11
- insertAt theString tickString 8
-
- return theTick
- )
-
- --*=============================================================================*
- --* Method name: setTime
- --* Class: DigitalClock
- --* Usage: setTime self timeFormat
- --* timeFormat - NameClass object (@12hour, @24hour)
- --*-----------------------------------------------------------------------------*
- --* Description: Changes the format IV and then sets the hour, minute, second
- --* and tick substrings of the target string to the calendar
- --* clock's time.
- --* Use this method to change the display format of the clock.
- --*=============================================================================*
- method setTime self {class DigitalClock} timeFormat ->
- (
- self.format := timeFormat
-
- --*=========================================================================*
- --* Set the clock time to the calendar clock time.
- --*=========================================================================*
- local calendarTime := theCalendarClock.date
- local clockTime := new Time
-
- clockTime.hours := calendarTime.hours
- clockTime.minutes := calendarTime.minutes
- clockTime.seconds := calendarTime.seconds
- clockTime.ticks := calendarTime.ticks
-
- self.clock.time := clockTime
-
- --*=========================================================================*
- --* Set the hour, minute, second and tick substring of the target string.
- --*=========================================================================*
- setHours self
- setMinutes self
- setSeconds self
- setTicks self
-
- --*=========================================================================*
- --* Start the clock.
- --*=========================================================================*
- self.clock.rate := 1
-
- return timeFormat
- )
-
- --*=============================================================================*
- --* Method name: reset
- --* Class: DigitalClock
- --* Usage: reset self
- --*-----------------------------------------------------------------------------*
- --* Description: Stops and resets the stopwatch.
- --*=============================================================================*
- method reset self {class DigitalClock} ->
- (
- if (self.mode = @clock) do
- return
-
- self.format := @24hours
-
- --*=========================================================================*
- --* Stop the clock and reset the clock time.
- --*=========================================================================*
- self.clock.rate := 0
- self.clock.time := 0
-
- --*=========================================================================*
- --* Set the hour, minute, second and tick substring of the target string
- --*=========================================================================*
- setHours self
- setMinutes self
- setSeconds self
- setTicks self
-
- return
- )
-
- --*=============================================================================*
- --* Method name: start
- --* Class: DigitalClock
- --* Usage: start self
- --*-----------------------------------------------------------------------------*
- --* Description: Starts the stopwatch.
- --*=============================================================================*
- method start self {class DigitalClock} ->
- (
- if (self.mode = @stopwatch) do
- self.clock.rate := 1
- )
-
- --*=============================================================================*
- --* Method name: stop
- --* Class: DigitalClock
- --* Usage: stop self
- --*-----------------------------------------------------------------------------*
- --* Description: Stop the stopwatch.
- --*=============================================================================*
- method stop self {class DigitalClock} ->
- (
- if (self.mode = @stopwatch) do
- self.clock.rate := 0
- )
-
- --*=============================================================================*
- --* Method name: modeGetter
- --* Class: DigitalClock
- --* Usage: modeGetter self
- --*-----------------------------------------------------------------------------*
- --* Description: Returns the mode (@clock, @stopwatch).
- --*=============================================================================*
- method modeGetter self {class DigitalClock} ->
- (
- return self._mode
- )
-
- --*=============================================================================*
- --* Method name: modeSetter
- --* Class: DigitalClock
- --* Usage: modeSetter self value
- --* value - nameclass (@clock, @stopwatch)
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the mode to either @clock or @stopwatch.
- --*=============================================================================*
- method modeSetter self {class DigitalClock} value ->
- (
- self._mode := value
-
- if (value = @stopwatch) then
- reset self
- else
- setTime self self.format
-
- return value
- )
-
- --*=============================================================================*
- --* Method name: updateClock
- --* Class: DigitalClock
- --* Usage: updateClock self
- --*-----------------------------------------------------------------------------*
- --* Description: Updates the hour, minute and second substrings of the
- --* target string. The second, minute and hour substrings gets
- --* updated every second, on the minute and on the hour
- --* respectively.
- --*=============================================================================*
- method updateClock self {class DigitalClock} ->
- (
- if ((setSeconds self) = 0) do
- if ((setMinutes self) = 0) do
- setHours self
-
- return self.target
- )
-
- --*=============================================================================*
- --* Method name: startClock
- --* Class: DigitalClock
- --* Usage: startClock self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the time then starts callbacks which update the clock
- --* every second and the ticks every 60th of a second
- --*=============================================================================*
- method startClock self {class DigitalClock} ->
- (
- if (self.mode = @clock) do
- setTime self self.format
-
- addPeriodicCallback self.clock updateClock self #() 60
- addPeriodicCallback self.clock setTicks self #() 1
- )
-
- --*=============================================================================*
- --* Method name: init
- --* Class: DigitalClock
- --* Usage: init self [boundary:<Stencil>]
- --* [format:<NameClass>] - (@12hour, @24hour)
- --* [mode:<NameClass>] - (@clock, @stopwatch)
- --*-----------------------------------------------------------------------------*
- --* Description: Creates and initializes the TextPresenter to its default
- --* values unless another is explicitly given.
- --* Default values:
- --* boundary - (new rect x2:94 y2:20)
- --* target - (new String string:"00:00:00:00")
- --* format - (@24hour)
- --* mode - (@clock)
- --*=============================================================================*
- method init self {class DigitalClock} #rest args \
- #key boundary:(new rect x2:94 y2:20) \
- format:(@24hour) \
- mode:(@clock) ->
- (
- apply nextmethod self boundary:boundary \
- target:(new String string:"00:00:00:00") args
- self.isTransparent := true
-
- self.format := format
- self.mode := mode
-
- self
- )
-
-